<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Hash;
use Gregwar\Captcha\CaptchaBuilder;
use Session;

class RegisterController extends Controller
{
    /**
     *     注册页面
     */
    public function index()
    {
        return view('layout/header');
    }

    /**
     *   生成验证码
     */
    public function vcode()
    {
        $builder = new CaptchaBuilder;
        //可以设置图片宽高及字体
        $builder->build($width = 90, $height = 35, $font = null);
        //获取验证码的内容
        $phrase = $builder->getPhrase();
        //把内容存入session
        Session::flash('vcode', $phrase);
        //生成图片
        header("Cache-Control: no-cache, must-revalidate");
        header('Content-Type: image/jpeg');
        $builder->output();

    }

    /**
     * 邮箱注册
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function register(Request $request)
    {
        $reg = $request->except('code_one');
        $email = $reg['email'];
        $username = $reg['username_one'];
        $password = Hash::make($reg['password_one']);
        $token = $reg['_token'];
        $register['email'] = $email;
        $register['username'] = $username;
        $register['nickname'] = $username;
        $register['token'] = $token;
        $register['password'] = $password;

        $res = DB::table('project_user')->insertGetId($register);

        $details['uid'] =  $res;
    
        $details['gold'] = 30;  //初次登陆赠30金币
        //注册 默认和最后登陆时间一直
        $details['regtime'] = $details['lasttime'] = time();
        //注册ip
        $details['regip'] = $request->ip();

        $result = DB::table('project_user_details')->insert($details);


        if($result){
            return redirect('/Home');
        }else{
            echo 'false';
        }
    }

    /**
     *  手机注册
     */
    public function doregister(Request $request)
    {
        $register = $request->except('repassword','code','check_code','_token');
        $token = $request->input('_token');
        $register['token'] = $token;
        $register['password'] = Hash::make($request->input('password'));
        $res = DB::table('project_user')->insertGetId($register);

        $details['uid'] =  $res;  //uid
    
        $details['gold'] = 30;  //初次登陆赠30金币
        //注册 默认和最后登陆时间一直
        $details['regtime'] = $details['lasttime'] = time();
        //注册ip
        $details['regip'] = $request->ip();

        $result = DB::table('project_user_details')->insert($details);

        if($result){
            return redirect('/Home');
        }else{
            echo 'false';
        }
    }

    /**
     *  手机验证ajax
     */
    public function dophone()
    {
        $phone = $_GET['phone'];
        $res = DB::table('project_user')->where('phone',$phone)->get();
        if(empty($res)){
            echo 'yes';
        }else{
            echo 'no';
        }
    }

    /**
     * @param Request $request
     *  验证码验证
     */
    public function docode(Request $request)
    {
        $vcode = session('vcode');
        $code = $request->input('code');
        if($code == $vcode){
            echo 'yes';
        }else{
            echo 'no';
        }
    }

    /**
     *  用户名验证
     */
    public function douser()
    {
        $username = $_GET['username'];
        $res = DB::table('project_user')->where('username',$username)->get();
        if(empty($res)){
            echo 'yes';
        }else{
            echo 'no';
        }
    }

    /**
     *  邮箱验证ajax
     */
    public function doemail()
    {
        $email = $_GET['email'];
        $res = DB::table('project_user')->where('email',$email)->get();
        if(empty($res)){
            echo 'yes';
        }else{
            echo 'no';
        }
    }


}